package com.design.pattern.command.command;

import java.util.Iterator;
import java.util.Stack;

/**
 * 长沙先飞信息科技有限公司<br>
 * Created with IntelliJ IDEA.<br>
 * Date:2024/6/20<br>
 * Time:12:24<br>
 * 功能描述：<br>
 *      由多条命令组合而成的命令
 * @author huang hou chun
 * @since JDK 1.8 <br>
 * Copyright (c) 2023 www.csxfrj.com System Incorporated All Rights Reserved.
 */
public class MacroCommand implements Command{

    // 命令的集合
    private final Stack<Command> commands = new Stack<>();

    @Override
    public void execute() {
        for (Command command : commands) {
            command.execute();
        }
    }

    // 添加命令
    public void append(Command command) {
        if (command != this) {
            commands.push(command);
        }
    }

    // 删除最后一条命令
    public void undo() {
        if (!commands.empty()) {
            commands.pop();
        }
    }

    // 删除所有命令
    public void clear() {
        commands.clear();
    }
}
